import Vue from 'vue'
import Router from 'vue-router'
// import Search from '@/pages/search/search'
// import Detail from '@/pages/detail/detail'
// import Score1 from '@/pages/score1/score1'
// import Score2 from '@/pages/score2/score2'
// import PreReport from '@/pages/pre-report/pre-report'
// import Report from '@/pages/report/report'
// import UserCenter from '@/pages/user-center/user-center'
// import MyAccount from '@/pages/my-account/my-account'
// import MyReports from '@/pages/my-reports/my-reports'
// import MyEvaluations from '@/pages/my-evaluations/my-evaluations'
// import MyFavorites from '@/pages/my-favorites/my-favorites'
// import PayDay from '@/pages/pay-day/pay-day'
// import PayMonth from '@/pages/pay-month/pay-month'
// import PayOnce from '@/pages/pay-once/pay-once'
// import PayHistory from '@/pages/pay-history/pay-history'
// import PayDetail from '@/pages/pay-detail/pay-detail'
// import Feedback from '@/pages/feedback/feedback'
// import BindMobile from '@/pages/bind-mobile/bind-mobile'

const Blank = () => import('@/pages/search/blank')

const Search = () => import('@/pages/search/search')
const Detail = () => import('@/pages/detail/detail')
const Score1 = () => import('@/pages/score1/score1')
const Score2 = () => import('@/pages/score2/score2')
const BasicDisease = () => import('@/pages/basic-disease/basic-disease')
const PreReport = () => import('@/pages/pre-report/pre-report')
const Report = () => import('@/pages/report/report')
const UserCenter = () => import('@/pages/user-center/user-center')
const MyAccount = () => import('@/pages/my-account/my-account')
const MyReports = () => import('@/pages/my-reports/my-reports')
const MyEvaluations = () => import('@/pages/my-evaluations/my-evaluations')
const MyFavorites = () => import('@/pages/my-favorites/my-favorites')
const PayDay = () => import('@/pages/pay-day/pay-day')
const PayMonth = () => import('@/pages/pay-month/pay-month')
const PayOnce = () => import('@/pages/pay-once/pay-once')
const PayHistory = () => import('@/pages/pay-history/pay-history')
const PayDetail = () => import('@/pages/pay-detail/pay-detail')
const Feedback = () => import('@/pages/feedback/feedback')
const BindMobile = () => import('@/pages/bind-mobile/bind-mobile')


import weixin from '../util/weixin.js'

Vue.use(Router)

const router = new Router({
	// mode: 'history',
	// saveScrollPosition: true,
	routes: [
		{
			path: '/blank',
			name: 'blank',
			component: Blank
		},
		{
			path: '/',
			name: 'index',
			component: Search
		},{
			path: '/detail',
			name: 'detail',
			component: Detail
		},{
			path: '/score1',
			name: 'score1',
			component: Score1
		},{
			path: '/score2',
			name: 'score2',
			component: Score2
		},{
			path: '/basic-disease',
			name: 'basicDisease',
			component: BasicDisease
		},{
			path: '/pre-report',
			name: 'preReport',
			component: PreReport
		},{
			path: '/report',
			name: 'report',
			component: Report
		},{
			path: '/report-demo',
			name: 'reportDemo',
			component: Report
		},{
			path: '/user-center',
			name: 'userCenter',
			component: UserCenter
		},{
			path: '/my-account',
			name: 'myAccount',
			component: MyAccount
		},{
			path: '/my-reports',
			name: 'myReports',
			component: MyReports
		},{
			path: '/my-evaluations',
			name: 'myEvaluations',
			component: MyEvaluations
		},{
			path: '/my-favorites',
			name: 'myFavorites',
			component: MyFavorites
		},{
			path: '/pay-day',
			name: 'payDay',
			component: PayDay
		},{
			path: '/pay-month',
			name: 'payMonth',
			component: PayMonth
		},{
			path: '/pay-once',
			name: 'payOnce',
			component: PayOnce
		},{
			path: '/pay-history',
			name: 'payHistory',
			component: PayHistory
		},{
			path: '/pay-detail',
			name: 'payDetail',
			component: PayDetail
		},{
			path: '/bind-mobile',
			name: 'bindMobile',
			component: BindMobile
		},{
			path: '/feedback',
			name: 'feedback',
			component: Feedback
		},{
			path: '*',
			name: 'notFound',
			component: Search
		}
	]
});

router.beforeEach((to, from, next) => {
	// 如果进入的页面不是综合评价首页,则禁止分享
	if(to.path!='/detail' && window.weixinShare && window.weixinShare.status!='deny'){
		if (window.getNewTicket()){
			window.weixinShare.shareDeny();
		} else {
			setTimeout(window.weixinShare.shareDeny,500);
		}
	}
	// 解决部分手机在用后退键关闭微信浏览器的过程中仍会露出"搜一搜"页面的bug
	if(to.path=='/' && from.path && from.path.length>1 && window.location.href.indexOf('route=')>-1){
		window.WeixinJSBridge && WeixinJSBridge.call('closeWindow');
		next({ path: '/blank' });
	} else if(to.path=='/' && from.path=='/detail' && window.forbidden==true){
		next(false); // 在雷达图页面环形图生长完成前,阻止用户后退回搜索结果页(因为这时可能造成触发多次后退事件)
	} else {
		// 判断window.$pages数组里是否含有下一跳的路由名称,如没有(路由懒加载,页面组件还未加载过),显示loading框
		let name=to.name;
		let arr=window.$pages||[];
		if(arr.indexOf(name)==-1){
			if(window.ui){
				window.ui.showLoading();
			}
		}
		next()
	}
});

router.afterEach((to, from) => {
	// 在数组里记录当前路由名称,并去除重复
	window.ui.hideLoading();
	let name=to.name;
	let arr=window.$pages || [];
	let set=new Set(arr);
	set.add(to.name);
	window.$pages=Array.from(set);
})

export default router
